package com.xd.core.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.common.util.XkdUtil;
import com.xd.core.business.entity.Surrenderinfo;
import com.xd.core.business.mapper.InformationMapper;
import com.xd.core.business.mapper.OutInfoMapper;
import com.xd.core.business.mapper.SurrenderinfoMapper;
import com.xd.core.business.service.ISurrenderinfoService;
import com.xd.core.business.vo.HisBillInfoVo;
import com.xd.core.business.vo.SurrenderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZX
 * @since 2025-08-26
 */
@Service
public class SurrenderinfoServiceImpl extends ServiceImpl<SurrenderinfoMapper, Surrenderinfo> implements ISurrenderinfoService {

    @Autowired
    private SurrenderinfoMapper surrenderinfoMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private OutInfoMapper outInfoMapper;

    @Override
    public SurrenderDetailVo getSurrenderDetail(Integer id) {
        SurrenderDetailVo detailVo = new SurrenderDetailVo();
        XkdUtil.checkNonNullProperties(id);
        //获取退保信息
        SurrenderDetailVo detail = surrenderinfoMapper.getSurrenderDetail(id);
        //根据退保信息id获取退保凭证url
        List<String> vouchers = informationMapper.getVouchers(id);
        detail.setInformationUrl(vouchers);
        //根据id获取退保前账单
        Surrenderinfo surrenderinfo = this.getById(id);
        String bills = surrenderinfo.getHistoryBills();
        //将退保前账单转换为还款计划集合
        List<HisBillInfoVo> hisBills = JSON.parseArray(bills, HisBillInfoVo.class);
        //获取借款方式
        String loanMethod = outInfoMapper.getLoanMethod(hisBills.get(0).getIncomingId());
        BeanUtil.copyProperties(detail, detailVo);
        detailVo.setBillInfoVo(hisBills);
        detailVo.setLoanMethodRemark(loanMethod);
        return detailVo;
    }
}
